import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/presentation/tools/tools_page/models/tool.dart';
import 'dart:io';
import '../../../../data/controllers/tools_controller.dart';

part 'add_tool_page_state.dart';

class AddToolPageCubit extends Cubit<AddToolPageState> {
  TextEditingController nameController = TextEditingController();
  TextEditingController addressController = TextEditingController();
  TextEditingController descriptionController = TextEditingController();
  TextEditingController workingHoursController = TextEditingController();

  final FocusNode nameFocusNode = FocusNode();
  final FocusNode addressFocusNode = FocusNode();
  final FocusNode workingHoursFocusNode = FocusNode();
  final FocusNode descriptionFocusNode = FocusNode();

  bool editMode = false;
  late String gymId;

  AddToolPageCubit() : super(AddToolPageInitial());

  static AddToolPageCubit get(context) => BlocProvider.of(context);
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  File? image;
  String? imageURL;


  void init(ToolModel? myTool) {
    if(myTool!=null){
      gymId = myTool.toolId;
      editMode = true;
      nameController.text = myTool.toolName ;
      descriptionController.text = myTool.toolDescription;
      if(myTool.image != null && myTool.image!.isNotEmpty){
        imageURL = myTool.image;
      }
      emit(AddToolPageUpdate());
    }else{
      editMode = false;
    }
  }

  void onChangeImage(File? image) {
    this.image = image;
    emit(AddToolPageUpdate());
  }


  clearOldData(){
     nameController.text = "";
     descriptionController.text = "";
     addressController.text = "";
     workingHoursController.text = "";
     image = null;
  }

  addGym(BuildContext context)async{
    var dialogLoading =  context;
     showDialog(context: context, builder: (context1){
       dialogLoading = context1;
       return const Center(child: CircularProgressIndicator());
     });
    ToolModel newGym = ToolModel(
      toolDescription: descriptionController.text.trim(),
      toolName: nameController.text.trim(),
    );


    emit(AddToolPageLoading());
    newGym.imageFile = image;

    await ToolsController.addTool(newGym).then((value){
      clearOldData();
    });
    Navigator.pop(dialogLoading);
    emit(AddToolPageDone());

  }


  updateGym(BuildContext context)async{
    var dialogLoading =  context;
    showDialog(context: context, builder: (context1){
      dialogLoading = context1;
      return const Center(child: CircularProgressIndicator());
    });
    ToolModel gym = ToolModel(
      imageFile: image,
      toolName: nameController.text.trim(),
      toolDescription: descriptionController.text.trim(),
    );

    emit(AddToolPageLoading());
    gym.imageFile = image;

    await ToolsController.updateTool(gym).then((value){});
    Navigator.pop(dialogLoading);
    emit(AddToolPageDone());
  }



  update(){
    emit(AddToolPageUpdate());
  }

  Future<void> selectDate(BuildContext context) async {
    final DateTime? pickedDate = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(1900),
      lastDate: DateTime.now(),
      builder: (BuildContext context, Widget? child) {
        return Theme(
          data: ThemeData(
            fontFamily: 'Tajawal',
            visualDensity: VisualDensity.standard,
            colorScheme: const ColorScheme.dark().copyWith(
              primary: Colors.red, // تحديد اليوم بلون أحمر
            ),
            textButtonTheme: TextButtonThemeData(
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
              ),
            ),
          ),
          child: child!,
        );
      },
    );

    if (pickedDate != null) {
      String formattedDate = DateFormat('yyyy-MM-dd').format(pickedDate);
      //datetimeController.text = formattedDate;
    }

    emit(AddToolPageUpdate());
  }

}
